package com.ykq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ykq.entity.Student;
import com.ykq.mapper.StudentMapper;
import com.ykq.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 闫克起
 * @Description:
 * @Date: Create in 9:36 2024/10/12
 */
@RestController //返回的都是json数据
@RequestMapping("/student")
public class StudentController {
    @Autowired  //根据spring为接口生成的代理实现类  帮你注入该属性的值。
    private StudentMapper studentMapper;
    //查询所有学生
    @GetMapping("/getById")
    public R getById(Integer id){
        Student student = studentMapper.selectByPrimaryKey(id);
        if(student!=null){
            return R.ok(student);
        }
        return R.error("查询失败");
    }
    //添加操作
    @PostMapping("/add")
    public R add(@RequestBody Student student){ //@RequestBody把前端的json数据转换为java对象
        int i = studentMapper.insertSelective(student);
        if(i>0){
            return new R(200,"添加成功");
        }
        return new R(500,"添加失败");
    }
    //修改
    @PutMapping("/update")
    public R update(@RequestBody Student student){

        int i = studentMapper.updateByPrimaryKeySelective(student);
        if(i>0){
            return new R(200,"修改成功");
        }
        return new R(500,"修改失败");
    }
    //删除操作
    @DeleteMapping("/delete")
    public R delete(Integer id){
        int i = studentMapper.deleteByPrimaryKey(id);
        if(i>0){
            return new R(200,"删除成功");
        }
        return new R(500,"删除失败");
    }

    @GetMapping("/list")
    public R list(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);//开启分页 pageNum   pageSize
        List<Student> students = studentMapper.selectAll();
        PageInfo<Student> pageInfo=new PageInfo<>(students);//把查询的结果封装到PageInfo类型
        return new R(200,"查询成功",pageInfo);
    }
}
//
//-------------------组长--创建项目架构---并提交远程仓库
/////////////////////组员--克隆项目架构---修改业务代码 ---本地仓库--拉取--提交远程仓库












